1029B - Creating the Contest - CodeForces Solution


dp greedy math *1200

Please click on ads to support us..

Python Code:

import sys
sys.setrecursionlimit(200000)
class UF:
  def __init__(self, n):
    self.root = [i for i in range(n)]
    self.subt = [1] * n

  def find(self, x):
    if self.root[x] == x:
      return x
    else:
      self.root[x] = self.find(self.root[x])
      return self.root[x]

  def union(self, x, y):
    x, y = self.find(x), self.find(y)
    x, y = min(x, y), max(x, y)
    if x != y:
      self.subt[x] += self.subt[y]
    self.root[y] = x

  def size(self, x):
    return self.subt[self.find(x)]

n = int(input())
a = list(map(int, input().split()))
uf = UF(n)
for i in range(n-1):
  if a[i+1] <= a[i] * 2:
    uf.union(i, i+1)
print(max(uf.subt))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int N = 2e5+5, MOD = 1e9+7;
const int dy[] = {0, 1, 0, -1, -1, -1, 1, 1};
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int i, j, k, t, n, a[N], ans;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(i = 0; i < n; ++i) cin >> a[i];
    for(i = 0, j = 0; i < n - 1; ++i) {
        if(a[i + 1] <= 2 * a[i]) j++;
        else {
            ans = max(ans, j + 1);
            j = 0;
        }
    }
    cout << max(ans, j + 1);
    return 0;
}


Comments

Submit
0 Comments
More Questions

165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts